<?php

/**
 * Handles a collection of ListItems
 * 
 * @author Ben Rowe
 * @see ListItem
 *
 */
class ListItemCollection extends Collection
{
	/**
	 * item hash that stores an array of ListItems
	 *
	 * @access private
	 * @var array
	 */
	var $_items = array();
	
	/**
	 * Add a ListItem at the end of the hash
	 *
	 * @access public
	 * @param ListItem $item
	 * @return boolean true on success
	 */
	function addItem($item)
	{
		if(is_a($item, 'ListItem')) {
			array_push($this->_items, $item);
			return true;
		}
		return false;
	}
	
	/**
	 * Add a ListItem at a specific point of the existing hash
	 *
	 * @access public
	 * @param integer $index
	 * @param ListItem $item
	 * @access boolean true on success
	 */
	function addItemAt($index, $item)
	{
		if(count($this->_items) > 0 && $index < count($this->_items)) {
			$new = array();
			for($i = 0; $i < count($this->_items); $i++) {
				if($index == $i) {
					array_push($new, $item);
				}
				array_push($new, $this->_items[$i]);
			}
			$this->_items = $new;
			return true;
		} else {
			$this->addItem($item);
			return true;
		}
		return false;
	}
	
	/**
	 * Retrieve an array of ListItem's
	 *
	 * @access public
	 * @return array
	 */
	function getItems()
	{
		return $this->_items;
	}
	
	/**
	 * Removes all elements from collection
	 *
	 * @access public
	 * @return boolean
	 */
	function clear()
	{
		$this->_items = array();
		return true;
	}
	
	function count()
	{
		return count($this->_items);
	}
}

?>